package com.ch.dsms_backend.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(urlPatterns = "/*",filterName="headerFilter")
public class HeaderFilter implements Filter{

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response=(HttpServletResponse)servletResponse;
        HttpServletRequest request=(HttpServletRequest) servletRequest;

        // 解决跨域访问start
        response.setContentType("text/html;charset=UTF-8");
        //允许跨域请求的域名
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Methods","POST, GET, OPTIONS, DELETE");

//        response.setHeader("Access-Control-Allow-Origin","*");

        response.setHeader("Access-Control-Max-Age","0");
        response.setHeader("Access-Control-Allow-Headers","Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,token");
        response.setHeader("Access-Control-Allow-Credentials","true");
        filterChain.doFilter(request, response);
    }
}

